'use strict';
const dayjs = require("dayjs");
const {getDefaultModelConfig} = require("../../utils/common");
const {formatDate, parseNum} = require("../../utils");

module.exports = app => {
    const { STRING, INTEGER, BIGINT, DECIMAL, DATE, TINYINT } = app.Sequelize;
    return app.model.define(
        'Coupon',
        {
            id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true },
            coupon_name: { type: STRING(100), allowNull: false },
            coupon_type: { type: TINYINT, allowNull: false },
            amount: { type: DECIMAL(10,2), allowNull: true },
            discount: { type: DECIMAL(3,2), allowNull: true },
            min_amount: { type: DECIMAL(10,2), allowNull: true,
                get() {
                    return  parseNum(this.getDataValue('min_amount'))
                },
            },
            start_time: {
                type: DATE, allowNull: false,
                get() {
                    return formatDate(this.getDataValue('start_time'), 'YYYY-MM-DD HH:mm:ss');
                },
            },
            end_time: {
                type: DATE, allowNull: false,
                get() {
                    return formatDate(this.getDataValue('end_time'), 'YYYY-MM-DD HH:mm:ss');
                },
            },
            total_count: { type: INTEGER, allowNull: true },
            remain_count: { type: INTEGER, allowNull: true },
            is_enabled: { type: TINYINT, allowNull: false, defaultValue: 1 },
            creator: { type: BIGINT, allowNull: false, defaultValue: 0 },
            updater: { type: BIGINT, allowNull: true },
            deleted: { type: TINYINT, allowNull: true, defaultValue: 0 },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'coupon',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );
}; 